// Parse.h: interface for the CParse class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_PARSE_H__39850F26_7932_11D2_8063_00A0C91F41A7__INCLUDED_)
#define AFX_PARSE_H__39850F26_7932_11D2_8063_00A0C91F41A7__INCLUDED_

#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000

#include "Lex.h"
#include "DirectiveSet.h"
#include "8085Object.h"
#include "InstructionSet.h"
#include "LabelTable.h"
#include "Unresolved.h"
#include "DebugData.h"

class CParse : public Lex 
{
public:
	int ParseAndInsertAt(word,char *);
	void AddUnresolvedJump(char *str,word addr);
	void ResolveUnresolvedSymbols();
	void InsertRst();
	void InsertCodeAnd8Bit();
	void InsertCode();
    void InsertCodeAnd16Bit(int validRegs=1);
	void InsertRegisterPair(int validRegs=1);
	void InsertRegisterAnd16Bit(int validRegs=1);
    void InsertRegister(int validRegs=1,int nFactor=1);
    void Insert2Registers(int validRegs=1);
	void InsertRegisterAnd8Bit(int validRegs=1);

    void SkipComma(BOOL bThrowException=TRUE);
	char * GetIdentifier(BOOL bThrowException=TRUE);
	word GetNumber(BOOL throwException=TRUE);
    word GetNumber(char *str,int base,BOOL throwException=TRUE);
	word ParseExpression(char *str,int nBase, BOOL bThrowException=TRUE);
	int AsciiToNum(char *str,int base);
	int RegToNumber(regs r1,regs r2);
	int RegToNumber(regs r);

	regs IdentifyRegister(int validRegs=1);
    regs IdentifyRegister(char *str,int validRegs=1);
	regs IdentifyRegisterPair(char *str,int validRegs=1);
	regs IdentifyRegisterPair(int validRegs=1);

	void Set8085Object(C8085Object *obj);
    inline C8085Object * Get8085Object()    { return m_p8085Object; }
	CParse();
    CParse(C8085Object *);
	virtual ~CParse();

    // Implementation
    void Init();
    virtual int Parse(char *s);   // parse the the string 's'
    virtual int ParseFile(const CString& fileName);
    inline CMyException & GetExceptionObject() { return m_CException; }
    CString GetErrorPrefix();

    // parse the instruction whose position in the symbol table is 'x'
    int ParseInstruction();
    int ParseDirective();
    int ParseRemainingLine(int x);

    int Open(const CString& file);
	FILE *OpenFile(const CString& szFileName,char *mode="rt");
    void Close();
    void Reset();

    virtual int IsInstruction(char *str);
    virtual int Advance();

public:
	char * GetDebugFileName();
	void WriteDebugInfo();
	word m_Ptr;

protected:
    char m_szFileName[_MAX_PATH+1];
    char m_szDebugFileName[_MAX_PATH+1]; // file containing debug information
    FILE * m_InFile;

    // data members
    BOOL			m_bEof;  // indicates end-of-file
    BOOL			m_bProcessingFile;
    CMyException	m_CException;
    CDebugData		m_cDebugData;

    CInstructionSet m_InstrTable;
    CDirectiveSet	m_Directive;
    CLabelTable		m_SymbolTable;
    C8085Object*	m_p8085Object;
    //address,int,label name,char *
    // since it is a 1 pass assembler there will be unresolved symbols
    CArray<Unresolved *, Unresolved *> m_UnresolvedJumps;
    char*			m_szTemporary;
};

#endif // !defined(AFX_PARSE_H__39850F26_7932_11D2_8063_00A0C91F41A7__INCLUDED_)
